package com.license.logic.persistence;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisSessionManager {
	
private static SqlSessionFactory sf;
	

	private SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis.config.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		sf = new SqlSessionFactoryBuilder().build(reader);
		return sf;
	} 
	// constructor will receive a myBatis sessionFactory object
	public MyBatisSessionManager() {
		
	}
	
	public MyBatisSessionManager(SqlSessionFactory containerSessionFactory) throws IOException {
		if (containerSessionFactory == null)
			System.err.println("Error: could not load myBatis sessionFactory");
		sf = getSqlSessionFactory();
	}
	
	public SqlSessionFactory getSessionFactory() throws IOException{
		return getSqlSessionFactory();
	}


}
